using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Chapter6CustomControlsCS
{    
    [ToolboxData("<{0}:State runat=server></{0}:State>")]
    public class State : WebControl
    {
        [Serializable()]
        public struct StateData
        {
         public string strCustNumber;
         public int intCustStatus;
        }
        StateData sData;
        private Book bk;

        void  State_Init(object sender, EventArgs e)
        {
            if (this.IsViewStateEnabled == true)
            {
                if (this.IsTrackingViewState == false)
                {
                    this.TrackViewState();
                }
                this.ViewState["custId"] = "A123";
                base.OnInit(e);
            }

            if (this.HasChildViewState == true &&
                this.IsChildControlStateCleared == false)
            {
                this.ClearChildState();
            }
            else if (this.HasChildViewState == true)
            {
                this.ClearChildViewState();
            }
            else if (this.IsChildControlStateCleared == false)
            {
                this.ClearChildControlState();
            }

            if (this.Page.RequiresControlState(this) == false)
            {
                this.Page.RegisterRequiresControlState(this);
            }
        }

        protected override object SaveControlState()
        {
            //sData.intCustStatus = 9;
            //sData.strCustNumber = "A49K72";
            //return sData;

            BookToString tcv = new BookToString();
	        string strSerializedBook;

            System.ComponentModel.TypeConverter tcv2;

            tcv2 = TypeDescriptor.GetConverter(bk);
            strSerializedBook = (string) tcv.ConvertFrom(null, null, bk);

            strSerializedBook = (string) tcv.ConvertFrom(null, null, bk);

            return strSerializedBook;
        }

        protected override void LoadControlState(object savedState)
        {
            sData = (StateData)savedState;
            BookToString tcv;
            object objDeserialized;

            tcv = new BookToString();
            objDeserialized = tcv.ConvertTo(null, null, savedState, null);
            bk = (Book)objDeserialized;
        }
              
        
        protected override void LoadViewState(object savedState)
        {        
        sData = (StateData) savedState;
        }

        protected override bool ViewStateIgnoresCase
        {
            get
            {
                return false;
            }
        }

        public Book GetBook(string strISBN)
        {
            TimeSpan TwentyMinutes;
            string strBookKey;
        
            strBookKey = "Book" + strISBN + this.Page.Session.SessionID;

            if (this.Page.Cache[strBookKey] == null)
            {
                bk = new Book(strISBN);
                TwentyMinutes = new TimeSpan(0, 20, 0);
                this.Page.Cache.Add(strBookKey, bk, null, DateTime.MaxValue, TwentyMinutes,
                                  System.Web.Caching.CacheItemPriority.Normal, null);
                this.ViewState["BookKey"] = strISBN;
            }
            else
            {
                bk = (Book) this.Page.Cache[strBookKey];
            }
            return bk;
        }

    }
}
